package controller;

import com.google.gson.Gson;
import dao.GoodDao;
import dao.impl.GoodDaoImpl;
import domain.Cart;
import domain.CartItem;
import domain.Goods;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CartServlet extends BaseController{
    private GoodDao dao = new GoodDaoImpl();

    /**
     * 加入购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数(GoodsId)
        Integer id = Integer.valueOf(req.getParameter("id"));
        //2.得到商品的信息
        Goods goods = dao.queryGoodsById(id);
        //3.把商品信息，转换成为 CartItem 商品项
        CartItem cartItem = new CartItem(goods.getId(), goods.getName(), 1, goods.getPrice(),goods.getPrice());
        //4.调用 cart.addItem(CartItem); 添加商品项
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);

        System.out.println(cart);

        //保存最后一次的添加的商品名称到session域中
        req.getSession().setAttribute("lastName",cartItem.getName());

        //5.重定向回原来商品所在的地址页面
        resp.sendRedirect("/HfsSale/pages/index/index.jsp");
    }



    /**
     * 删除商品项
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取商品编号
        Integer id = Integer.valueOf(req.getParameter("id"));
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            //删除购物车的商品项
            cart.deleteItem(id);
            //重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }


    /**
     * 清空购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            //清空购物车
            cart.clear();
            //重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 修改商品数量
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数 商品的编号，商品数量
        Integer id = Integer.valueOf(req.getParameter("id"));
        Integer count = Integer.valueOf(req.getParameter("count"));
        //获取cart对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            //修改商品数量
            cart.updateCount(id,count);
            //重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
}
